<?php 
/**
 * SwordMouse CMS
 * $Id: site.php 5 2010-08-02 18:27:51Z dan@inkping.com $
 * @package SwordMouseClases
 */
class SITE {
	/**
	 * Required as part of the settings/include file and called by every script/page.
	 */
	public static function startScript(){
		$default = array(TYPE, HOST, PORT, BASE, CHAR, USER, PASS);
		$dbc = $this->dbconnect($default);
		//start session - use php built in session handler to hold global site settings 
		session_start();
		if(!isset($_SESSION['siteSettings'])){
			$siteSettings = array();
			$query = $dbc->prepare("SELECT * FROM settings");
			$query->execute();
			while($row = $query->fetch()){
				$key = $row['key'];
				$value = $row['value'];
				$siteSettings["$key"] = $value;
			}
			$_SESSION['siteSettings'] = $siteSettings;
		}
	}
	
	/**
	 * PDO database connection wrapper function.
	 * //FIXME does this need to be modified to be singleton to prevent multiple persistent connections????
	 * @param array $dsn - array containing database connection settings
	 */
	public static function dbconnect($dsn){
		$type = $dsn[0];
		$host = $dsn[1];
		$port = $dsn[2];
		$base = $dsn[3];
		$char = $dsn[4];
		$user = $dsn[5];
		$pass = $dsn[6];
		$dbc = new PDO($type.':host='.$host.';port='.$port.';dbname='.$base.';charset='.$char, $user, $pass);
	}
	
	/**
	 * Get site setting from $_SESSION array
	 * @return int|string - value of setting
	 * @param string $setting - setting to be retrieved from session array
	 */
	public static function getSetting($setting){
		$setting = strtolower($setting);
		$value = $_SESSION["$setting"];
		return $value;
	}
	
	/**
	 * Builds full base URL to reduce unnecessary repetition of code.
	 * Optionally accepts two parameters.
	 * @param string $secure - replace default prefix with default secure prefix. 
	 * @param string $directory - directory to append to URL
	 */
	public static function getUrl($secure = 'URL', $directory = ''){
		if($secure == 'SURL'){
			$prefix = SITE::getSetting('secureprefix');
		} else {
			$prefix = SITE::getSetting('prefix');
		}
		$URL = $prefix."://".SITE::getSetting('domain')."/".$directory."/";
		return $URL;
	}
	
	/**
	 * Checks if user has required admin level with settings override access then
	 * sets new value.
	 */
	public static function setSetting($setting){
		strtolower($setting);
		/*update db entry*/
	}
}   
?>